package com.example.hasee.zhcd.Base;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.NetUtil.HttpUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 作者：王林波
 * 时间：2016/10/25 11:04
 * 包名：com.cn.pinganyigou.base
 * 类描述：BaseFragment
 */
public abstract class BaseFragment extends Fragment implements View.OnClickListener{

    //主控件
    public View view;

    //工具
    public HttpUtils httpUtils;
//    public AllUtils allUtils;
//    public LoadDialog loadDialog;

    //参数
    public String className; //类名


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (view == null) {
            EventBus.getDefault().register(this);

            //工具初始化
            httpUtils = HttpUtils.getInstance(getActivity());
//            loadDialog = new LoadDialog(getActivity());

            //参数基础设置
            className = getActivity().getPackageName() + "." + getActivity().getLocalClassName();
//            allUtils = AllUtils.getInstance();
            view = inflater.inflate(contentViewId(), null);
            init(view);
            set();
        } else {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (null != parent) {
                parent.removeView(view);
            }
        }

        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//反注册EventBus
    }


    /**
     * 消息发到主线程ui界面
     */
    public abstract void getEventData(EventMsg msg);

    /**
     * eventbus 主线程回调
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventMsg msg) {
        getEventData(msg);
    }

    /**
     * 说明当前操作的layout的视图文件
     *
     * @return
     */
    public abstract int contentViewId();

    /**
     * 给页面组件添加数据
     */
    public abstract void set();

    /**
     * 初始化页面组件
     */
    public abstract void init(View view);

    @Override
    public void onResume() {
        super.onResume();
    }
}
